package com.suyu.util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;

import com.suyu.model.entity.TopicCuit;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author tmh
 * @date 2023/11/4 20:43
 * @description
 */
public class InsertData {
    public static ArrayList<TopicCuit> excelToCsvAnswer() {
        File file = null;
        try {
            file = ResourceUtils.getFile("classpath:suyu_data.xlsx");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //读取数据
        List<LinkedHashMap<Integer, String>> list = null;
        ArrayList<TopicCuit> topicList = new ArrayList<>();
        try {
            list = EasyExcel.read(file)
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet()
                    .headRowNumber(0)
                    .doReadSync();
            TopicCuit topic = new TopicCuit();


            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).values().contains("题号") || list.get(i).size() < 3) {
                    continue;
                }
                topic.setTopicNumber(list.get(i).get(0));
                topic.setTopDescription(list.get(i).get(1));
                topic.setType(list.get(i).get(2));
                topic.setOptionA(list.get(i).get(3));
                topic.setOptionB(list.get(i).get(4));
                topic.setOptionC(list.get(i).get(5));
                topic.setOptionD(list.get(i).get(6));
                topic.setCorrectAnswer(list.get(i).get(7));
//                System.out.println(list.get(i).values());
//                System.out.println(topic);
                topicList.add(topic);
                topic = new TopicCuit();
            }
            topicList.sort((o1, o2) -> Integer.valueOf(o1.getTopicNumber()) - Integer.valueOf(o2.getTopicNumber()));
/*            for (TopicCuit t : topicList) {
                System.out.println(t);
            }*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        return topicList;

    }

    public static void main(String[] args) {
        ArrayList<TopicCuit> topicCuits = excelToCsvAnswer();
        for (TopicCuit topicCuit : topicCuits) {
            System.out.println(topicCuit);
        }
//        System.out.println(topicCuits);
    }
}
